package net.huashitong.supervision.task.service;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.date.DateUtils;
import net.huashitong.supervision.insideDuCha.dto.RemindDto;
import net.huashitong.supervision.insideDuCha.entity.InsideCycleEntity;
import net.huashitong.supervision.insideDuCha.entity.InsideSubTaskEntity;
import net.huashitong.supervision.insideDuCha.entity.InsideUserTaskEntity;
import net.huashitong.supervision.insideDuCha.filter.InsideMainTaskFilter;
import net.huashitong.supervision.insideDuCha.service.InsideCycleService;
import net.huashitong.supervision.insideDuCha.service.InsideMainTaskService;
import net.huashitong.supervision.insideDuCha.service.InsideSubTaskService;
import net.huashitong.supervision.insideDuCha.service.InsideUserTaskService;
import net.huashitong.supervision.insideDuCha.util.CommUtil;
import net.huashitong.synthetical.showView.util.DataTimeUtil;
import net.huashitong.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static net.huashitong.util.DateUtil.dateFormat;

/**
 * 单位内部人员调度
 * 定时扫描任务
 */
@Service
public class TaskUserScheduleService {

    @Autowired
    private InsideUserTaskService userTaskService;
    @Autowired
    private InsideMainTaskService mainTaskService;
    @Autowired
    private InsideSubTaskService subTaskService;

    @Autowired
    private InsideCycleService insideCycleService;



    public void updateTaskUserRemind(){

        //首先拿到所有有效提醒事项
        List<InsideCycleEntity> listCycles = insideCycleService.getListCycles();

        for (InsideCycleEntity item : listCycles) {

            String remindDate = item.getRemindDate();
            String currentDate = DateUtil.getCurrentDate();
            if(currentDate.equals(remindDate))
            {
                String nextDate = nextRemindDate(item.getTimeType(), item.getCycleValue(), item.getRemindDate());
                int timeRound = item.getTimeRound();
                timeRound=timeRound+1;
                createNextUsTsk(nextDate,timeRound,item);
                //更新
                item.setTimeRound(timeRound);
                item.setRemindDate(nextDate);
                insideCycleService.update(item);
                //
                InsideSubTaskEntity subTask = subTaskService.getById(item.getSubTaskId());
                subTask.setTimeRound(timeRound);
                subTask.setRemindDate(nextDate);
                subTaskService.update(subTask);
            }
        }

        //List<RemindDto> cycleDetailInfos = insideCycleService.getCycleDetailInfos(listCycles);




    }



    /**
     * 保存新的人员任务；
     * @param nextDate
     * @param timeRound
     * @param dto
     */
    private void createNextUsTsk(String nextDate,int timeRound,InsideCycleEntity dto){
        String subTaskId = dto.getSubTaskId();
        List<InsideUserTaskEntity> userTasks = userTaskService.getUserTaskBySubId(subTaskId);
//        List<InsideUserTaskEntity> listTask = new ArrayList<>();
        for (InsideUserTaskEntity userTask : userTasks) {
            InsideUserTaskEntity entity = new InsideUserTaskEntity();
            BeanUtils.copyProperties(userTask,entity);
            entity.setTaskState(0+"");
            entity.setBackType(0+"");
            entity.setRemindDate(nextDate);
            entity.setTimeRound(timeRound);
            entity.setSysId(null);
            userTaskService.save(entity,userTask.getCreateUser());

            userTask.setIsCycle(1);
            userTaskService.update(userTask);
        }
//        userTaskService.batchSave(listTask);

    }


    /**
     * 计算下一次时间，没用
     * @param entity
     */
    public void  updateCalculateNextDate(InsideCycleEntity entity){

        String timeType = entity.getTimeType();
        entity.setValids(0+"");
        String remindDate = entity.getRemindDate();
        Calendar ca = Calendar.getInstance();
        Date alertDate = DateUtil.strToDate(remindDate);
        ca.setTime(alertDate);
        String newAlert ="";

            switch (timeType){

                case "2":
                    //周期性 天 当前日期加上天数 换算得到新日期
                    String cycleValue = entity.getCycleValue();
                    int val = Integer.parseInt(cycleValue);
                    ca.add(Calendar.DATE,val);

                    newAlert = DateUtil.DateToStr(ca.getTime());
                    break;

                case "3":
                    //周期性 周  提醒日期加 7天

                    ca.add(Calendar.DATE,7);
                    newAlert =  DateUtil.DateToStr(ca.getTime());

                    break;
                case "4":
                    // 周期性 月 加上一个月，考虑 30号 和2月的关系
                    ca.add(Calendar.MONTH,1);
                    newAlert =DateUtil.DateToStr(ca.getTime());
                    break;
                case "5":
                    //周期性季度 加上一个季度
                    ca.add(Calendar.MONTH,3);
                    newAlert =DateUtil.DateToStr(ca.getTime());

                    break;
                case "6":
                    //多时限 直接一次性把所有的 时限生成；
                   // String[] split = entity.getCycleValue().split(",");

                    break;
            }

    }

    /**
     * 计算下一个周期
     * @param timeType
     * @param cycleValue
     * @param remindDate
     * @return
     */
    private String nextRemindDate(String timeType,String cycleValue,String remindDate)
    {
        Calendar ca = Calendar.getInstance();
        Date alertDate = DateUtil.strToDate(remindDate);
        ca.setTime(alertDate);
        String newAlert ="";
        switch (timeType){

            case "2":
                //周期性 天 当前日期加上天数 换算得到新日期
                int val = Integer.parseInt(cycleValue);
                ca.add(Calendar.DATE,val);

                newAlert = DateUtil.DateToStr(ca.getTime());
                break;

            case "3":
                //周期性 周  提醒日期加 7天

                ca.add(Calendar.DATE,7);
                newAlert =  DateUtil.DateToStr(ca.getTime());

                break;
            case "4":
                // 周期性 月 加上一个月，考虑 30号 和2月的关系
                ca.add(Calendar.MONTH,1);
                newAlert =DateUtil.DateToStr(ca.getTime());
                break;
            case "5":
                //周期性季度 加上一个季度
                ca.add(Calendar.MONTH,3);
                newAlert =DateUtil.DateToStr(ca.getTime());

                break;
            case "6":
                //多时限 直接一次性把所有的 时限生成；
                // String[] split = entity.getCycleValue().split(",");

                break;
        }
        newAlert= dateFormat(newAlert,"yyyy-MM-dd");

        return  newAlert;
    }
}
